﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TouristAgencyDataLayer
{
    public class HotelFacilities
    {
        public static void CreateHotelFacility(string fac)
        {
            if (!string.IsNullOrEmpty(fac))
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                HotelFacility f = new HotelFacility()
                {
                    HotelFacility1 = fac
                };

                context.HotelFacilities.InsertOnSubmit(f);
                context.SubmitChanges();
            }
        }

        public static void UpdateFacility(int facId, string fac)
        {
            if (!string.IsNullOrEmpty(fac))
            {
                TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

                var res =
                    (from f in context.HotelFacilities
                     where f.HotelFacilityId == facId
                     select f).FirstOrDefault();

                if (res != null)
                {
                    res.HotelFacility1 = fac;
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteFacility(int facId)
        {

            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var fac =
                (from f in context.HotelFacilities
                 where f.HotelFacilityId == facId
                 select f).FirstOrDefault();

            if (fac != null)
            {
                var tHF =
                    from t in context.ToursHotelFacilities
                    where t.HotelFacilityId == facId
                    select t;

                foreach (var t in tHF)
                {
                    ToursHotelFacilities.DeleteTourHotelFacility(t.TourId, t.HotelFacilityId);
                }

                context.HotelFacilities.DeleteOnSubmit(fac);
                context.SubmitChanges();
            }
        }

        public static IQueryable<HotelFacility> GetHotelFacilities()
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            return context.HotelFacilities;
        }

        public static HotelFacility GetHotelFacilityById(int id)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                (from h in context.HotelFacilities
                 where h.HotelFacilityId == id
                 select h).FirstOrDefault();

            if (res != null)
            {
                return res;
            }
            throw new ArgumentException("Invalid Hotel facility id");
        }

        public static IQueryable<HotelFacility> GetFacilitiesForTour(int id)
        {
            TouristAgencyDataClassesDataContext context = new TouristAgencyDataClassesDataContext();

            var res =
                from th in context.ToursHotelFacilities
                where th.TourId == id
                join hf in context.HotelFacilities on th.HotelFacilityId equals hf.HotelFacilityId
                select hf;

            return res;
        }
    }
}
